import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
var _excluded = ["children", "onChange", "onBlur", "ignoreFormItem", "valuePropName"],
    _excluded2 = ["children", "addonAfter", "addonBefore", "valuePropName", "convertValue"],
    _excluded3 = ["valueType", "transform", "dataFormat", "ignoreFormItem", "lightProps", "children"];
import { createElement as _createElement } from "react";
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { isDropdownValueType, omitUndefined } from '@ant-design/pro-utils';
import { ConfigProvider, Form } from 'antd-v4';
import React, { useCallback, useContext, useEffect, useMemo } from 'react';
import { LightWrapper } from '../../BaseForm';
import FieldContext from '../../FieldContext';
import { FormListContext } from '../List';
var FormItemProvide = /*#__PURE__*/React.createContext({});
/**
 * 把value扔给 fieldProps，方便给自定义用
 *
 * @param param0
 * @returns
 */

var WithValueFomFiledProps = function WithValueFomFiledProps(formFieldProps) {
  var filedChildren = formFieldProps.children,
      onChange = formFieldProps.onChange,
      onBlur = formFieldProps.onBlur,
      ignoreFormItem = formFieldProps.ignoreFormItem,
      _formFieldProps$value = formFieldProps.valuePropName,
      valuePropName = _formFieldProps$value === void 0 ? 'value' : _formFieldProps$value,
      restProps = _objectWithoutProperties(formFieldProps, _excluded);

  var onChangeMemo = useCallback(function () {
    var _filedChildren$type, _filedChildren$props, _filedChildren$props$, _filedChildren$props2, _filedChildren$props3, _filedChildren$props4;

    for (var _len = arguments.length, restParams = new Array(_len), _key = 0; _key < _len; _key++) {
      restParams[_key] = arguments[_key];
    }

    onChange === null || onChange === void 0 ? void 0 : onChange.apply(void 0, restParams); // @ts-ignore

    if ((filedChildren === null || filedChildren === void 0 ? void 0 : (_filedChildren$type = filedChildren.type) === null || _filedChildren$type === void 0 ? void 0 : _filedChildren$type.displayName) !== 'ProFormComponent') return;
    if (! /*#__PURE__*/React.isValidElement(filedChildren)) return undefined;
    filedChildren === null || filedChildren === void 0 ? void 0 : (_filedChildren$props = filedChildren.props) === null || _filedChildren$props === void 0 ? void 0 : (_filedChildren$props$ = _filedChildren$props.onChange) === null || _filedChildren$props$ === void 0 ? void 0 : _filedChildren$props$.call.apply(_filedChildren$props$, [_filedChildren$props].concat(restParams));
    filedChildren === null || filedChildren === void 0 ? void 0 : (_filedChildren$props2 = filedChildren.props) === null || _filedChildren$props2 === void 0 ? void 0 : (_filedChildren$props3 = _filedChildren$props2.fieldProps) === null || _filedChildren$props3 === void 0 ? void 0 : (_filedChildren$props4 = _filedChildren$props3.onChange) === null || _filedChildren$props4 === void 0 ? void 0 : _filedChildren$props4.call.apply(_filedChildren$props4, [_filedChildren$props3].concat(restParams));
  }, [filedChildren, onChange]);
  var onBlurMemo = useCallback(function () {
    var _filedChildren$type2, _filedChildren$props5, _filedChildren$props6, _filedChildren$props7, _filedChildren$props8, _filedChildren$props9;

    // @ts-ignore
    if ((filedChildren === null || filedChildren === void 0 ? void 0 : (_filedChildren$type2 = filedChildren.type) === null || _filedChildren$type2 === void 0 ? void 0 : _filedChildren$type2.displayName) !== 'ProFormComponent') return;
    if (! /*#__PURE__*/React.isValidElement(filedChildren)) return;

    for (var _len2 = arguments.length, restParams = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
      restParams[_key2] = arguments[_key2];
    }

    onBlur === null || onBlur === void 0 ? void 0 : onBlur.apply(void 0, restParams);
    filedChildren === null || filedChildren === void 0 ? void 0 : (_filedChildren$props5 = filedChildren.props) === null || _filedChildren$props5 === void 0 ? void 0 : (_filedChildren$props6 = _filedChildren$props5.onBlur) === null || _filedChildren$props6 === void 0 ? void 0 : _filedChildren$props6.call.apply(_filedChildren$props6, [_filedChildren$props5].concat(restParams));
    filedChildren === null || filedChildren === void 0 ? void 0 : (_filedChildren$props7 = filedChildren.props) === null || _filedChildren$props7 === void 0 ? void 0 : (_filedChildren$props8 = _filedChildren$props7.fieldProps) === null || _filedChildren$props8 === void 0 ? void 0 : (_filedChildren$props9 = _filedChildren$props8.onBlur) === null || _filedChildren$props9 === void 0 ? void 0 : _filedChildren$props9.call.apply(_filedChildren$props9, [_filedChildren$props8].concat(restParams));
  }, [filedChildren, onBlur]);
  var fieldProps = useMemo(function () {
    var _filedChildren$type3, _filedChildren$props10;

    // @ts-ignore
    if ((filedChildren === null || filedChildren === void 0 ? void 0 : (_filedChildren$type3 = filedChildren.type) === null || _filedChildren$type3 === void 0 ? void 0 : _filedChildren$type3.displayName) !== 'ProFormComponent') return undefined;
    if (! /*#__PURE__*/React.isValidElement(filedChildren)) return undefined;
    return omitUndefined(_objectSpread(_objectSpread(_defineProperty({
      id: restProps.id
    }, valuePropName, formFieldProps[valuePropName]), (filedChildren === null || filedChildren === void 0 ? void 0 : (_filedChildren$props10 = filedChildren.props) === null || _filedChildren$props10 === void 0 ? void 0 : _filedChildren$props10.fieldProps) || {}), {}, {
      onBlur: onBlurMemo,
      // 这个 onChange 是 Form.Item 添加上的，
      // 要通过 fieldProps 透传给 ProField 调用
      onChange: onChangeMemo
    }));
  }, [filedChildren, formFieldProps, onBlurMemo, onChangeMemo, restProps.id, valuePropName]);
  var finalChange = useMemo(function () {
    if (fieldProps) return undefined;
    if (! /*#__PURE__*/React.isValidElement(filedChildren)) return undefined;
    return function () {
      var _filedChildren$props11, _filedChildren$props12;

      for (var _len3 = arguments.length, restParams = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
        restParams[_key3] = arguments[_key3];
      }

      onChange === null || onChange === void 0 ? void 0 : onChange.apply(void 0, restParams);
      filedChildren === null || filedChildren === void 0 ? void 0 : (_filedChildren$props11 = filedChildren.props) === null || _filedChildren$props11 === void 0 ? void 0 : (_filedChildren$props12 = _filedChildren$props11.onChange) === null || _filedChildren$props12 === void 0 ? void 0 : _filedChildren$props12.call.apply(_filedChildren$props12, [_filedChildren$props11].concat(restParams));
    };
  }, [fieldProps, filedChildren, onChange]);
  if (! /*#__PURE__*/React.isValidElement(filedChildren)) return _jsx(_Fragment, {
    children: filedChildren
  });
  return /*#__PURE__*/React.cloneElement(filedChildren, omitUndefined(_objectSpread(_objectSpread(_objectSpread({}, restProps), {}, _defineProperty({}, valuePropName, formFieldProps[valuePropName]), filedChildren.props), {}, {
    onChange: finalChange,
    fieldProps: fieldProps
  })));
};
/**
 * 支持了一下前置 dom 和后置的 dom 同时包一个provide
 *
 * @param WarpFormItemProps
 * @returns
 */


var WarpFormItem = function WarpFormItem(_ref) {
  var children = _ref.children,
      addonAfter = _ref.addonAfter,
      addonBefore = _ref.addonBefore,
      valuePropName = _ref.valuePropName,
      convertValue = _ref.convertValue,
      props = _objectWithoutProperties(_ref, _excluded2);

  var formDom = useMemo(function () {
    var getValuePropsFunc = function getValuePropsFunc(value) {
      var _convertValue;

      var newValue = (_convertValue = convertValue === null || convertValue === void 0 ? void 0 : convertValue(value, props.name)) !== null && _convertValue !== void 0 ? _convertValue : value;
      if (props.getValueProps) return props.getValueProps(newValue);
      return _defineProperty({}, valuePropName || 'value', newValue);
    };

    if (!convertValue && !props.getValueProps) {
      getValuePropsFunc = undefined;
    }

    if (!addonAfter && !addonBefore) {
      return _jsx(Form.Item, _objectSpread(_objectSpread({}, props), {}, {
        valuePropName: valuePropName,
        getValueProps: getValuePropsFunc,
        children: children
      }));
    }

    return _jsx(Form.Item // @ts-ignore
    , _objectSpread(_objectSpread({
      // @ts-ignore
      _internalItemRender: {
        mark: 'pro_table_render',
        render: function render(inputProps, doms) {
          return _jsxs(_Fragment, {
            children: [_jsxs("div", {
              style: {
                display: 'flex',
                alignItems: 'center'
              },
              children: [addonBefore ? _jsx("div", {
                style: {
                  marginRight: 8
                },
                children: addonBefore
              }) : null, doms.input, addonAfter ? _jsx("div", {
                style: {
                  marginLeft: 8
                },
                children: addonAfter
              }) : null]
            }), doms.extra, doms.errorList]
          });
        }
      }
    }, props), {}, {
      getValueProps: getValuePropsFunc,
      children: children
    })); // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [addonAfter, addonBefore, children, convertValue === null || convertValue === void 0 ? void 0 : convertValue.toString(), props]);
  return _jsx(FormItemProvide.Provider, {
    value: {
      name: props.name,
      label: props.label
    },
    children: formDom
  });
};

var ProFormItem = function ProFormItem(props) {
  var _rest$name2, _rest$name3, _rest$name4;

  /** 从 context 中拿到的值 */
  var size = useContext(ConfigProvider.SizeContext);

  var valueType = props.valueType,
      transform = props.transform,
      dataFormat = props.dataFormat,
      ignoreFormItem = props.ignoreFormItem,
      _props$lightProps = props.lightProps,
      lightProps = _props$lightProps === void 0 ? {} : _props$lightProps,
      unusedChildren = props.children,
      rest = _objectWithoutProperties(props, _excluded3);

  var formListField = useContext(FormListContext); // ProFromList 的 filed，里面有name和key

  /** 从 context 中拿到的值 */

  var name = useMemo(function () {
    if (formListField.name !== undefined) {
      return [formListField.name, props.name].flat(1);
    }

    return props.name;
  }, [formListField.name, props.name]);
  /** 从 context 中拿到的值 */

  var _React$useContext = React.useContext(FieldContext),
      setFieldValueType = _React$useContext.setFieldValueType,
      formItemProps = _React$useContext.formItemProps;

  useEffect(function () {
    // 如果 setFieldValueType 和 props.name 不存在不存入
    if (!setFieldValueType || !props.name) {
      return;
    } // Field.type === 'ProField' 时 props 里面是有 valueType 的，所以要设置一下
    // 写一个 ts 比较麻烦，用 any 顶一下


    setFieldValueType([formListField.listName, props.name].flat(1).filter(function (itemName) {
      return itemName !== undefined;
    }), {
      valueType: valueType || 'text',
      dateFormat: dataFormat,
      transform: transform
    });
  }, [formListField.listName, name, dataFormat, props.name, setFieldValueType, transform, valueType]);
  var isDropdown = /*#__PURE__*/React.isValidElement(props.children) && isDropdownValueType(valueType || props.children.props.valueType);
  var noLightFormItem = useMemo(function () {
    if (!lightProps.light || lightProps.customLightMode || isDropdown) {
      return true;
    }

    return false;
  }, [lightProps.customLightMode, isDropdown, lightProps.light]); // formItem 支持function，如果是function 我就直接不管了

  if (typeof props.children === 'function') {
    var _rest$name;

    return /*#__PURE__*/_createElement(WarpFormItem, _objectSpread(_objectSpread({}, rest), {}, {
      name: name,
      key: rest.proFormFieldKey || ((_rest$name = rest.name) === null || _rest$name === void 0 ? void 0 : _rest$name.toString())
    }), props.children);
  }

  var children = _jsx(WithValueFomFiledProps, {
    valuePropName: props.valuePropName,
    children: props.children
  }, rest.proFormFieldKey || ((_rest$name2 = rest.name) === null || _rest$name2 === void 0 ? void 0 : _rest$name2.toString()));

  var lightDom = noLightFormItem ? children : /*#__PURE__*/_createElement(LightWrapper, _objectSpread(_objectSpread({}, lightProps), {}, {
    key: rest.proFormFieldKey || ((_rest$name3 = rest.name) === null || _rest$name3 === void 0 ? void 0 : _rest$name3.toString()),
    size: size
  }), children); // 这里控制是否需要 LightWrapper，为了提升一点点性能

  if (ignoreFormItem) {
    return _jsx(_Fragment, {
      children: lightDom
    });
  }

  return _jsx(WarpFormItem, _objectSpread(_objectSpread(_objectSpread({}, formItemProps), rest), {}, {
    name: name,
    isListField: formListField.name !== undefined,
    children: lightDom
  }), rest.proFormFieldKey || ((_rest$name4 = rest.name) === null || _rest$name4 === void 0 ? void 0 : _rest$name4.toString()));
};

export { FormItemProvide };
export default ProFormItem;